package com.joysuch.wwyt.workflow.repository;

import com.joysuch.wwyt.workflow.entity.WorkFlowTask;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

public interface WorkFlowTaskDao extends JpaRepository<WorkFlowTask, Long> {


    List<WorkFlowTask> findAllByTaskIdIn(List<String> taskIdList);

    WorkFlowTask save(WorkFlowTask workFlowTask);

    WorkFlowTask findByTaskId(String taskId);

    WorkFlowTask findByProcessInstanceId(String processInstanceId);

    Page<WorkFlowTask> findAllByProcessIdAndStatusOrderByUpdateTimeDesc(Long workFlowId, Integer status, Pageable pageable);

    List<WorkFlowTask> findByParentId(Long parentId);

    List<WorkFlowTask> findByProcessId(Long processId);

    List<WorkFlowTask> findByProcessIdAndParentId(Long processId, Long parentId);

    @Query(value = "select * from wwyt_workflow_task where delete_flag = '0'and process_id =?1 and parent_id=?2 and status !=5 order by update_time desc", nativeQuery = true)
    List<WorkFlowTask> findScreenTaskByProcessIdAndParentId(Long processId, Long parentId);

    Page<WorkFlowTask> findByProcessIdInAndParentId(List<Long> processIdList, Long parentId, Pageable pageable);

    List<WorkFlowTask> findByProcessIdAndTaskNameInAndParentId(Long processId, List<String> taskNameList, Long parentId);

    @Query(value = "select * from wwyt_workflow_task where delete_flag = '0'and process_id =?1 and task_name in (?2) and parent_id=?3 and status !=5 order by update_time desc", nativeQuery = true)
    List<WorkFlowTask> findScreenTaskByParam(Long processId, List<String> taskNameList, Long parentId);

    @Query(value = "select * from wwyt_workflow_task where delete_flag = '0'and id in :pids", nativeQuery = true)
    List<WorkFlowTask> findByProcessIds(@Param("pids") List<Long> pids);

    List<WorkFlowTask> findAllByCode(String taskCode);

    @Query(value = "select * from wwyt_workflow_task where delete_flag = '0' and code =?1 and parent_id = ?2 limit 1", nativeQuery = true)
    Optional<WorkFlowTask> findOneByCodeAndParentId(String taskCode, Long parentId);

    @Query(value = "select * from wwyt_workflow_task where   id =?1 ", nativeQuery = true)
    Optional<WorkFlowTask> findById(Long id);

    @Query(value = "select * from wwyt_workflow_task where delete_flag = '0' and process_id =?1 and status=?2 and level_name IS NOT NULL ", nativeQuery = true)
    List<WorkFlowTask> findScreenTaskByProcessIdAndStatus(Long processId, Integer status);
    @Modifying
    @Query(value = "UPDATE wwyt_workflow_task SET level_name = ?2,push_time=?3 WHERE id = ?1", nativeQuery = true)
    void setlevelNameById(Long id, String levelName, LocalDateTime now);
}
